﻿using System;
using System.Windows.Forms;

namespace CAN_Controller
{
    public partial class SettingsForm : Form
    {
        public SettingsForm()
        {
            InitializeComponent();
            devType_Box.SelectedIndex = 0;
            devIndex_Box.SelectedIndex = 0;
            CANIndex_Box.SelectedIndex = 0;
            rate_Box.SelectedIndex = 7;
            sendType_Box.SelectedIndex = 0;
            externFlag_Box.SelectedIndex = 0;
            remoteFlag_Box.SelectedIndex = 0;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //设备类型
            if (devType_Box.SelectedIndex == 0)
            {
                MainForm.mDevType = 4;
            }
            else
            {
                MainForm.mDevType = 5;
            }
            //设备索引号
            MainForm.mDevIndex = (uint)devIndex_Box.SelectedIndex;
            //通道号
            MainForm.mCANIndex = (uint)CANIndex_Box.SelectedIndex;
            //波特率
            switch (rate_Box.SelectedIndex)
            {
                case 0:
                    {//5kbps
                        MainForm.mConfig.Timing0 = 0xBF;
                        MainForm.mConfig.Timing0 = 0xFF;
                    }
                    break;
                case 1:
                    {//10kbps
                        MainForm.mConfig.Timing0 = 0x31;
                        MainForm.mConfig.Timing0 = 0x1C;
                    }
                    break;
                case 2:
                    {//20kbps
                        MainForm.mConfig.Timing0 = 0x18;
                        MainForm.mConfig.Timing0 = 0x1C;
                    }
                    break;
                case 3:
                    {//40kbps
                        MainForm.mConfig.Timing0 = 0x87;
                        MainForm.mConfig.Timing0 = 0xFF;
                    }
                    break;
                case 4:
                    {//50kbps
                        MainForm.mConfig.Timing0 = 0x09;
                        MainForm.mConfig.Timing0 = 0x1C;
                    }
                    break;
                case 5:
                    {//80kbps
                        MainForm.mConfig.Timing0 = 0x83;
                        MainForm.mConfig.Timing0 = 0Xff;
                    }
                    break;
                case 6:
                    {//100kbps
                        MainForm.mConfig.Timing0 = 0x04;
                        MainForm.mConfig.Timing0 = 0x1C;
                    }
                    break;
                case 7:
                    {//125kbps
                        MainForm.mConfig.Timing0 = 0x03;
                        MainForm.mConfig.Timing0 = 0x1C;
                    }
                    break;
                case 8:
                    {//200kbps
                        MainForm.mConfig.Timing0 = 0x81;
                        MainForm.mConfig.Timing0 = 0xFA;
                    }
                    break;
                case 9:
                    {//250kbps
                        MainForm.mConfig.Timing0 = 0x01;
                        MainForm.mConfig.Timing0 = 0x1C;
                    }
                    break;
                case 10:
                    {//400kbps
                        MainForm.mConfig.Timing0 = 0x80;
                        MainForm.mConfig.Timing0 = 0xFA;
                    }
                    break;
                case 11:
                    {//500kbps
                        MainForm.mConfig.Timing0 = 0x00;
                        MainForm.mConfig.Timing0 = 0x1C;
                    }
                    break;
                case 12:
                    {//666kbps
                        MainForm.mConfig.Timing0 = 0x80;
                        MainForm.mConfig.Timing0 = 0xB6;
                    }
                    break;
                case 13:
                    {//800kbps
                        MainForm.mConfig.Timing0 = 0x00;
                        MainForm.mConfig.Timing0 = 0x16;
                    }
                    break;
                case 14:
                    {//1000kbps
                        MainForm.mConfig.Timing0 = 0x00;
                        MainForm.mConfig.Timing0 = 0x14;
                    }
                    break;
            }
            //发送方式
            MainForm.mSendType = (uint)sendType_Box.SelectedIndex;
            //帧类型
            MainForm.mExternFlag = (uint)externFlag_Box.SelectedIndex;
            //帧格式
            MainForm.mRemoteFlag = (uint)remoteFlag_Box.SelectedIndex;

            //reset通道0
            ExternalLib.VCI_ResetCAN(MainForm.mDevType, MainForm.mDevIndex, 0);
            //reset通道1
            ExternalLib.VCI_ResetCAN(MainForm.mDevType, MainForm.mDevIndex, 1);
            //初始化并打开设备0通道0            
            if (ExternalLib.VCI_InitCAN(MainForm.mDevType, MainForm.mDevIndex, /*CANIndex*/ 0, ref MainForm.mConfig) == 0)
                Console.WriteLine("0 init fail");
            if (ExternalLib.VCI_StartCAN(MainForm.mDevType, MainForm.mDevIndex,/*CANIndex*/ 0) == 0)
                Console.WriteLine("0 start fail");
            //初始化并打开设备0通道1
            if (ExternalLib.VCI_InitCAN(MainForm.mDevType, MainForm.mDevIndex,/*CANIndex*/ 1, ref MainForm.mConfig) == 0)
                Console.WriteLine("1 init fail");
            if (ExternalLib.VCI_StartCAN(MainForm.mDevType, MainForm.mDevIndex,/*CANIndex*/ 1) == 0)
                Console.WriteLine("1 start fail");
            Close();
        }
    }
}
